<template>
  <div class="login-container">
    <!-- 登录表单 -->
    <el-card class="login-card" shadow="always">
      <template #header>
        <div class="card-header">
          <el-icon class="login-icon" size="48" color="#409EFF">
            <UserFilled />
          </el-icon>
          <h2>系统登录</h2>
          <p>请输入您的用户名和密码</p>
        </div>
      </template>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        size="large"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            <el-icon v-if="!loading"><Right /></el-icon>
            {{ loading ? "登录中..." : "登录" }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 测试账号信息 -->
      <el-divider content-position="center">测试账号</el-divider>

      <div class="test-accounts">
        <el-row :gutter="12">
          <el-col :span="12">
            <el-card
              class="account-card"
              shadow="hover"
              @click="fillAccount('admin')"
            >
              <div class="account-info">
                <el-icon color="#F56C6C"><Star /></el-icon>
                <div>
                  <div class="account-title">管理员</div>
                  <div class="account-detail">admin / 123456</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="12">
            <el-card
              class="account-card"
              shadow="hover"
              @click="fillAccount('user')"
            >
              <div class="account-info">
                <el-icon color="#409EFF"><User /></el-icon>
                <div>
                  <div class="account-title">普通用户</div>
                  <div class="account-detail">user001 / 123456</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Lock, Right, UserFilled, Star } from "@element-plus/icons-vue";
import { useUserStore } from "@/stores/user";

const router = useRouter();
const userStore = useUserStore();

// 响应式数据
const loading = ref(false);
const loginFormRef = ref();

const loginForm = reactive({
  username: "",
  password: "",
});

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    {
      min: 2,
      max: 20,
      message: "用户名长度在 2 到 20 个字符",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在 6 到 20 个字符", trigger: "blur" },
  ],
};

// 填充测试账号
const fillAccount = (type) => {
  if (type === "admin") {
    loginForm.username = "admin";
    loginForm.password = "123456";
  } else {
    loginForm.username = "user001";
    loginForm.password = "123456";
  }
  ElMessage.success("已填充测试账号信息");
};

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return;

  try {
    const valid = await loginFormRef.value.validate();
    if (!valid) return;
  } catch {
    return;
  }

  loading.value = true;

  try {
    await userStore.login(loginForm);
    ElMessage.success("登录成功！");

    // 清空表单
    loginForm.username = "";
    loginForm.password = "";
    loginFormRef.value?.resetFields();

    // 直接跳转到客户管理页面
    router.push("/dashboard/customers");
  } catch (error) {
    ElMessage.error(error.message || "登录失败");
  } finally {
    loading.value = false;
  }
};

// 组件挂载时验证令牌并跳转
onMounted(async () => {
  const isValid = await userStore.verifyToken();
  if (isValid) {
    router.push("/dashboard/customers");
  }
});
</script>

<style lang="scss" scoped>
// 变量定义
$primary-color: #409eff;
$success-color: #67c23a;
$danger-color: #f56c6c;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$border-color: #e4e7ed;
$bg-color: #f5f7fa;

.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 50px;
  padding-left: 20px;
  padding-right: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  border-radius: 12px;
}

.card-header {
  text-align: center;
  padding: 20px 0;

  .login-icon {
    margin-bottom: 16px;
  }

  h2 {
    margin: 0 0 8px 0;
    font-size: 24px;
    font-weight: 600;
    color: $text-primary;
  }

  p {
    margin: 0;
    color: $text-secondary;
    font-size: 14px;
  }
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.test-accounts {
  margin-top: 16px;

  .account-card {
    cursor: pointer;
    transition: all 0.3s ease;
    border-radius: 8px;

    &:hover {
      transform: translateY(-2px);
    }
  }

  .account-info {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px;

    .account-title {
      font-size: 12px;
      font-weight: 500;
      color: $text-primary;
      line-height: 1.2;
    }

    .account-detail {
      font-size: 10px;
      color: $text-secondary;
      font-family: "Courier New", monospace;
      line-height: 1.2;
    }
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-container {
    padding: 16px;
  }

  .login-card {
    margin: 0;
  }

  .test-accounts .account-info {
    flex-direction: column;
    text-align: center;
    gap: 4px;
  }
}

// Element Plus 组件自定义样式
:deep(.el-input__inner) {
  height: 44px;
  font-size: 14px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-divider__text) {
  font-size: 12px;
  color: $text-secondary;
}

:deep(.el-card__header) {
  padding: 16px 20px;
  border-bottom: 1px solid $border-color;
}

:deep(.el-card__body) {
  padding: 24px 20px;
}
</style>
